import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'

// 导入视图组件
import OverviewView from '@/components/views/OverviewView.vue'
import ResourcesView from '@/components/views/ResourcesView.vue'
import BuildingsView from '@/components/views/BuildingsView.vue'
import TechnologyView from '@/components/views/TechnologyView.vue'
import ShipsView from '@/components/views/ShipsView.vue'
import DiplomacyView from '@/components/views/DiplomacyView.vue'

// 路由配置
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/overview'
  },
  {
    path: '/overview',
    name: 'Overview',
    component: OverviewView,
    meta: {
      title: '概览',
      icon: '🏠',
      description: '帝国总览和统计信息'
    }
  },
  {
    path: '/resources',
    name: 'Resources',
    component: ResourcesView,
    meta: {
      title: '资源',
      icon: '📦',
      description: '资源管理和贸易'
    }
  },
  {
    path: '/buildings',
    name: 'Buildings',
    component: BuildingsView,
    meta: {
      title: '建筑',
      icon: '🏗️',
      description: '建筑建造和管理'
    }
  },
  {
    path: '/technology',
    name: 'Technology',
    component: TechnologyView,
    meta: {
      title: '科技',
      icon: '🔬',
      description: '科技研究和发展'
    }
  },
  {
    path: '/ships',
    name: 'Ships',
    component: ShipsView,
    meta: {
      title: '舰队',
      icon: '🚀',
      description: '舰船设计和舰队管理'
    }
  },
  {
    path: '/diplomacy',
    name: 'Diplomacy',
    component: DiplomacyView,
    meta: {
      title: '外交',
      icon: '🤝',
      description: '外交关系和贸易协议'
    }
  },
  // 子路由和特殊页面
  {
    path: '/galaxy',
    name: 'Galaxy',
    component: () => import('@/components/views/GalaxyView.vue'),
    meta: {
      title: '银河系',
      icon: '🌌',
      description: '银河系地图和探索'
    }
  },
  {
    path: '/colonies',
    name: 'Colonies',
    component: () => import('@/components/views/ColoniesView.vue'),
    meta: {
      title: '殖民地',
      icon: '🌍',
      description: '殖民地管理'
    }
  },
  {
    path: '/research',
    name: 'Research',
    component: () => import('@/components/views/ResearchView.vue'),
    meta: {
      title: '研究',
      icon: '🧪',
      description: '科研项目管理'
    }
  },
  {
    path: '/military',
    name: 'Military',
    component: () => import('@/components/views/MilitaryView.vue'),
    meta: {
      title: '军事',
      icon: '⚔️',
      description: '军事单位和战争'
    }
  },
  {
    path: '/trade',
    name: 'Trade',
    component: () => import('@/components/views/TradeView.vue'),
    meta: {
      title: '贸易',
      icon: '💰',
      description: '贸易路线和市场'
    }
  },
  {
    path: '/settings',
    name: 'Settings',
    component: () => import('@/components/views/SettingsView.vue'),
    meta: {
      title: '设置',
      icon: '⚙️',
      description: '游戏设置和选项'
    }
  },
  // 错误页面
  {
    path: '/404',
    name: 'NotFound',
    component: () => import('@/components/views/NotFoundView.vue'),
    meta: {
      title: '页面未找到',
      icon: '❌',
      description: '请求的页面不存在'
    }
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404'
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 路由切换时的滚动行为
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta?.title) {
    document.title = `${to.meta.title} - 星际帝国`
  } else {
    document.title = '星际帝国'
  }
  
  // 这里可以添加权限检查、登录验证等逻辑
  // 目前直接放行
  next()
})

router.afterEach((to, from) => {
  // 路由切换后的处理
  console.log(`路由从 ${from.path} 切换到 ${to.path}`)
})

// 导出路由实例和路由配置
export default router
export { routes }

// 导出路由相关的类型和工具函数
export interface RouteMetaInfo {
  title: string
  icon: string
  description: string
  requiresAuth?: boolean
  roles?: string[]
}

// 获取主要导航路由
export function getMainNavRoutes() {
  return routes.filter(route => 
    route.path !== '/' && 
    route.path !== '/404' && 
    !route.path.includes('*') &&
    route.meta?.title &&
    !route.path.startsWith('/settings')
  )
}

// 获取管理类路由
export function getManagementRoutes() {
  const managementPaths = ['/colonies', '/research', '/military', '/trade']
  return routes.filter(route => 
    managementPaths.includes(route.path) && route.meta?.title
  )
}

// 获取系统路由
export function getSystemRoutes() {
  const systemPaths = ['/settings']
  return routes.filter(route => 
    systemPaths.includes(route.path) && route.meta?.title
  )
}

// 根据路由名称获取路由信息
export function getRouteByName(name: string) {
  return routes.find(route => route.name === name)
}

// 检查当前路由是否匹配指定路径
export function isRouteActive(currentPath: string, targetPath: string): boolean {
  if (targetPath === '/') {
    return currentPath === '/' || currentPath === '/overview'
  }
  return currentPath.startsWith(targetPath)
}